<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '产线'"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-row>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="产线名称"
                prop="name"
                hasFeedback
              >
                <a-input
                  placeholder="请输入产线名称"
                  v-model.trim="model.name"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="产线编号"
                hasFeedback
                prop="serialNumber"
              >
                <a-input
                  disabled
                  placeholder="请输入产线编号"
                  v-model.trim="model.serialNumber"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="产线负责人"
                hasFeedback
                prop="principalId"
              >
                <a-select
                  :open="false"
                  :defaultOpen="false"
                  mode="multiple"
                  :allowClear="true"
                  placeholder="请选择产线负责人"
                  v-model.trim="model.principalId"
                  @dropdownVisibleChange="
                    (open) =>
                      handleTableSelect(
                        open,
                        'principalId',
                        'tableUserModal',
                        '员工'
                      )
                  "
                  @change="
                    (value) => handleChange(value, 'principalId', userModel)
                  "
                >
                  <a-select-option
                    v-for="item in userModel.tableOption"
                    :key="item.id"
                    :value="item.id"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="产线位置"
                hasFeedback
                prop="location"
              >
                <a-input
                  placeholder="请输入产线位置"
                  v-model.trim="model.location"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="选择设备"
                hasFeedback
                prop="equipIds"
              >
                <a-select
                  :open="false"
                  :defaultOpen="false"
                  mode="multiple"
                  :allowClear="true"
                  placeholder="请选择设备"
                  v-model.trim="model.equipIds"
                  @dropdownVisibleChange="
                    (open) =>
                      handleTableSelect(
                        open,
                        'equipIds',
                        'tableEquipmentModal',
                        '设备'
                      )
                  "
                  @change="
                    (value) => handleChange(value, 'equipIds', equipmentModel)
                  "
                >
                  <a-select-option
                    v-for="item in equipmentModel.tableOption"
                    :key="item.id"
                    :value="item.id"
                  >
                    {{ item.equipmentNumber }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-spin>
    </a-modal>
    <!-- 选择员工弹框 -->
    <TableSelectModal
      :columns="userModel.columns"
      :replaceFields="userModel.replaceFields"
      :tableKey="userModel.tableKey"
      :treeKey="userModel.treeKey"
      :searchKey="userModel.searchKey"
      :name="userModel.name"
      :code="userModel.code"
      :filename="userModel.filename"
      ref="tableUserModal"
      @tableSelect="tableUserSelect"
    />
    <!-- 选择设备弹框 -->
    <TableSelectModal
      :columns="equipmentModel.columns"
      :replaceFields="equipmentModel.replaceFields"
      :treeKey="equipmentModel.treeKey"
      :searchKey="equipmentModel.searchKey"
      :tableKey="equipmentModel.tableKey"
      :name="equipmentModel.name"
      :code="equipmentModel.code"
      :filename="equipmentModel.filename"
      ref="tableEquipmentModal"
      :mode="equipmentModel.mode"
      @tableSelect="tableEquipmentSelect"
    />
  </div>
</template>

<script>
import api from "@/api/apiServer";
import {
  userModel,
  equipmentModel,
} from "@/pages/EquipmentProductionLine/components/tableConfig";
import TableSelectModal from "@/components/TableSelectModal/index.vue";
export default {
  components: { TableSelectModal },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 900, // 弹框宽度
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      userModel,
      equipmentModel,
    };
  },
  computed: {
    rules() {
      return {
        name: [
          { required: true, message: "请输入产线名称", trigger: "change" },
        ],
        serialNumber: [
          { required: true, message: "请输入产线编号", trigger: "change" },
        ],
        principalId: [
          { required: true, message: "请选择产线负责人", trigger: "change" },
        ],
        location: [
          { required: true, message: "请输入产线位置", trigger: "change" },
        ],
        equipIds: [
          { required: true, message: "请选择设备", trigger: "change" },
        ],
      };
    },
  },
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.model = Object.assign({}, record);
      if (!record.id) {
        this.getProductLineNumber();
      }
    },
    // 获取产品线编号
    async getProductLineNumber() {
      const res = await api.ProductLineNumber();
      this.$set(this.model, "serialNumber", res.data);
    },
    // 选择负责人
    handleTableSelect(open, key, refName, name) {
      this.$refs[refName].title = name;
      this.$refs[refName].modalAdd();
      if (this.model[key]) {
        let arr = this.model[key].toString().split(",");
        if (arr.length > 0) {
          let newArr = arr.map((item) => {
            return Number(item);
          });
          this.$refs[refName].selectedRowKeys = newArr;
        } else {
          this.$refs[refName].selectedRowKeys = [];
        }
      }
    },
    // 事件选择回调
    handleChange(value, key, model) {
      if (value.length > 0) {
        if (model.mode) {
          this.model[key] = value;
        } else {
          this.model[key] = value.toString();
        }
      } else {
        delete this.model[key];
      }
    },
    // 选择责任人
    tableUserSelect(result, tableOption) {
      this.setData(result, tableOption, "userModel", "principalId");
    },
    // 选择设备
    tableEquipmentSelect(result, tableOption) {
      this.setData(result, tableOption, "equipmentModel", "equipIds");
    },
    // 处理选择组件的数据
    setData(result, tableOption, modelName, key) {
      this[modelName].tableOption = tableOption;
      if (this[modelName].mode) {
        let arr = result.map((item) => {
          return item.id;
        });
        this.$set(this.model, key, arr);
      } else {
        this.$set(this.model, key, result[0].id);
      }
      this.$refs.ruleForm.clearValidate([key]);
    },
    async submitVulFun() {
      this.confirmLoading = true;
      let res = await api.ProductLineUpdate(this.model);
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        this.model = Object.assign({}, this.model);
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.model = Object.assign({});
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 900;
      }
    },
  },
};
</script>

<style></style>
